/*
 * Copyright 2007, 2008 Tiberiumlabs
 *
 * This file is part of Tiberiumlabs Lailaps.
 *
 * Tiberiumlabs Lailaps is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Tiberiumlabs Lailaps is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.tiberiumlabs.lailaps.gui.table;

import java.awt.Color;
import java.awt.Component;
import javax.swing.JLabel;
import javax.swing.JTable;

/**
 *
 * @author <a href="mailto:paranoid.tiberiumlabs@gmail.com">Paranoid</a>
 */
public class TableCellRenderer extends JLabel implements javax.swing.table.TableCellRenderer {

    private static Color ALTERNATIVE_BACKGROUND = new Color(240, 240, 240);

    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {

        setFont(table.getFont());

        if (isSelected) {
            setForeground(table.getSelectionForeground());
            setBackground(table.getSelectionBackground());
        }
        else {
            setForeground(table.getForeground());
            if (row % 2 != 0) {
                setBackground(ALTERNATIVE_BACKGROUND);
            }
            else {
                setBackground(table.getBackground());
            }
        }

        setValue(value);

        return this;
    }

    protected void setValue(Object value) {
        if (value == null) {
            setText(null);
            setToolTipText(null);
            return;
        }
        String v = value.toString();
        setText(v);
        setToolTipText(v);
    }

    @Override
    public boolean isOpaque() {
        return true;
    }

    // <editor-fold defaultstate="collapsed" desc=" see DefaultTableCellRenderer implementation note ">
    @Override
    public void revalidate() {
    }
    @Override
    public void invalidate() {
    }
    @Override
    public void validate() {
    }
    @Override
    public void repaint() {
    }
    @Override
    public void repaint(long tm) {
    }
    @Override
    public void repaint(int x, int y, int width, int height) {
    }
    @Override
    public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) {
    }
    @Override
    protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
    }
    // </editor-fold>

}
